package org.bits.io;

import org.bits.BitsResources;
import org.bits.data.*;
import org.owasp.validator.html.*;

public class FindingHtmlSerializer
{
    public String serialize( Finding finding )
    {
        StringBuilder text = new StringBuilder( 2048 );

        try
        {
            text.append( "<html><table align=center border=0><tr><td>" );

            text.append( "<table width=700 border=0>" );
            text.append( "<tr>" );

            text.append( "<td width=100 align=center>" );
            text.append( finding.number );
            text.append( "</td>" );

            text.append( "<td align=center>" );
            text.append( "<b>" );
            text.append( finding.title );
            text.append( "</b>" );
            text.append( "</td>" );

            text.append( "<td width=100 align=center>" );
            text.append( finding.state );
            text.append( "</td>" );

            text.append( "</tr>" );
            text.append( "<tr>" );

            text.append( "<td colspan=3 align=right>" );
            text.append( "Created by " );
            text.append( finding.creator.getFirstLast() );
            text.append( " on " );
            text.append( finding.creationDate );
            text.append( "</td>" );

            text.append( "</tr>" );

            if( finding.state != FindingState.NEW )
            {
                text.append( "<tr>" );

                text.append( "<td colspan=3 align=right>" );
                text.append( "Assigned to " );
                text.append( finding.assignee.getFirstLast() );
                text.append( "</td>" );

                text.append( "</tr>" );
            }
            text.append( "</table>" );

            text.append( "Versions:" );
            text.append( "<br><div style=\"margin-left:10px;\">" );
            for( Version v : finding.versions )
            {
                text.append( v );
                text.append( "<br>" );
            }
            text.append( "</div>" );

            text.append( "<br>" );
            text.append( finding.description );
            text.append( "<br>" );

            for( Note n : finding.notes )
            {
                text.append( "<br>" );
                text.append( "<div style='background:#e5e5e5;'><div class='comments-post'>" );
                text.append( "On " );
                text.append( n.date );
                text.append( ", " );
                text.append( n.author.getFirstLast() );
                text.append( " said:</div>" );
                // text.append( "<br>" );
                text.append( toSafeHtml( n.text ).replaceAll( "\\n|\\r",
                    "<br />" ) );
                text.append( "</div>" );
            }

            text.append( "</td></tr></table>" );
            text.append( "</html>" );
        }
        catch( ScanException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch( PolicyException e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return text.toString();
    }

    private String toSafeHtml( String html ) throws ScanException,
        PolicyException
    {
        Policy policy = Policy.getInstance( BitsResources.loader.getUrl( "antisamy-bits.xml" ) );
        AntiSamy antiSamy = new AntiSamy();
        CleanResults cleanResults = antiSamy.scan( html, policy );
        return cleanResults.getCleanHTML().trim();
    }
}
